package org.example.kafka;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author : 叶荻风
 * @create : 2024/6/12 20:10
 */
public class Consumer {
    public static void main(String[] args) {

        Map<String,Object> config = new HashMap<>();
        config.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"localhost:9092");
        config.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        config.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,StringDeserializer.class.getName());
        config.put(ConsumerConfig.GROUP_ID_CONFIG,"atguigu");
        KafkaConsumer<String,String> consumer = new KafkaConsumer<>(config);
        consumer.subscribe(Collections.singletonList("test"));
        while(true){
            ConsumerRecords<String, String> poll = consumer.poll(100);

            for (ConsumerRecord<String, String> stringStringConsumerRecord : poll) {
                System.out.println(stringStringConsumerRecord);
            }

        }

       // consumer.close();
    }
}
